//
//  MarkerApi.swift
//  amap
//
//  Created by James Karl on 2019/9/12.
//

import Foundation
import MAMapKit

class MarkerApi : FlutterApi {
    
    func handle(mapView: MAMapView, methodId: String, data: Any?) -> ReplyToFlutter? {
        var result: ReplyToFlutter? = nil
        switch methodId {
        case "addMarker":
            result = addMarker(mapView: mapView, data: data)
        case "addMarkers":
            result = addMarkers(mapView: mapView, data: data)
        case "addCircle":
            result = addCircle(mapView: mapView, data: data)
        case "addPolyline":
            result = addPolyline(mapView: mapView, data: data)
        case  "addPolygon":
            result = addPolygon(data: data)
        case "clear":
            result = clear(mapView: mapView)
        default:
            result = nil
        }
        return result
    }
    
    
    private func addCircle(mapView: MAMapView, data: Any?) ->  ReplyToFlutter {
        if let json = data as? NSDictionary {
            let circleOptions = CircleOptionsData.init(dict: json)
            if let overlay = CircleOverlay(circleOptions) {
                mapView.add(overlay)
                return ReplyToFlutter.success()
            }
        }
        return ReplyToFlutter.notImplemented()
    }
    
    private func addPolyline(mapView: MAMapView, data: Any?) ->  ReplyToFlutter {
        if let jsonData = data as? [String: Any?] {
            if let polylineOptionData = PolylineOptionsData(json: jsonData) {
                if let overlay = PolylineOverlay(options: polylineOptionData) {
                    mapView.add(overlay)
                    return ReplyToFlutter.success()
                }
            }
        }
        return ReplyToFlutter.failed()
    }
    
    private func addPolygon(data: Any?) ->  ReplyToFlutter {
        //        return ReplyToFlutter.Failed(message = "TODO")
        return ReplyToFlutter.notImplemented()
    }
    
    private func clear(mapView: MAMapView) ->  ReplyToFlutter {
        mapView.removeAnnotations(mapView.annotations)
        mapView.removeOverlays(mapView.overlays)
        return ReplyToFlutter.success()
    }
    
    private func addMarker(mapView: MAMapView, data: Any?) ->  ReplyToFlutter {
        if let markerData = data as? [String: Any?] {
            if let marker = AnnotationViewData(json: markerData) {
                if let annotation = MapAnnotation(options: marker) {
                    mapView.addAnnotation(annotation)
                    return ReplyToFlutter.success()
                }
            }
        }
        return ReplyToFlutter.failed()
    }
    
    private func addMarkers(mapView: MAMapView, data: Any?) ->  ReplyToFlutter {
        if let markersData = data as? [[String: Any?]] {
            var markers:[MapAnnotation] = []
            markersData.forEach({json in
                if let marker = AnnotationViewData(json: json) {
                    if let annotation = MapAnnotation(options: marker) {
                        markers.append(annotation)
                    }
                }
            })
            if markers.isEmpty == false {
                mapView.addAnnotations(markers)
                return ReplyToFlutter.success()
            }
        }
        return ReplyToFlutter.failed()
    }
    
    private func setInfoWindowAdapter(mapView: MAMapView) -> ReplyToFlutter {
        //        PlatformMapView.infoWindowAdapter?.let {
        //            map.setInfoWindowAdapter(it)
        //        }
        return ReplyToFlutter.notImplemented()
    }
    
    //    private func attachMarkerIcon(markerOptionData: MarkerOptionData, markerOptions: MarkerOptions) {
    //        markerOptionData.icon?.let { icon ->
    //            val cachedBitmapDescriptor = cache.get(icon)
    //            if (cachedBitmapDescriptor != null) {
    //                markerOptions.icon(cachedBitmapDescriptor)
    //            } else {
    //                PlatformMapView.getFlutterAsset(icon)?.let { fd ->
    //                    val bitmap = BitmapFactory.decodeStream(fd.createInputStream())
    //                    val bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap)
    //                    cache.put(icon, bitmapDescriptor)
    //                    Log.d("MAP", "create new marker icon identified by $icon")
    //                    markerOptions.icon(bitmapDescriptor)
    //                }
    //            }
    //        }
    //
    //        val activity = PlatformMapView.getActivity()
    //        val markerIconFactory = PlatformMapView.markerIconFactory
    //        if (markerOptionData.icon == null && activity != null && markerIconFactory != null) {
    //            markerIconFactory.createMarkerIcon(activity, markerOptionData)?.let { view ->
    //                val bitmap = convertViewToBitmap(view)
    //                Log.d("MAP", "create bitmap from view $bitmap")
    //                val bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap)
    //                markerOptions.icon(bitmapDescriptor)
    //            }
    //        }
    
    //    }
    
    //    private func convertViewToBitmap(view: View) ->  Bitmap {
    //        view.measure(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)
    //        val width = view.measuredWidth
    //        val height = view.measuredHeight
    //        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    //        val canvas = Canvas(bitmap)
    //        view.layout(0, 0, width, height)
    //        view.draw(canvas)
    //        return bitmap
    //    }
}
